using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class DressingRoomMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.dr_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	public DressingRoomMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnMenuClose += OnMenuClose;
			menu.OnItemSelect += OnItemSelect;
			isMenuInitialized = true;
		}
	}

	public static void InitDressingRoom()
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_Model));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_Components));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_Props));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_Tattoo));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_Badge));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_AdvancedSettings));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_SaveSkin));
		menu.AddItem(new UIMenuItem(TextSetting.dr_MenuItem_LoadSkin));
		menu.BindMenuToItem(ModelMenu.menu, menu.MenuItems[0]);
		menu.BindMenuToItem(ComponentMenu.menu, menu.MenuItems[1]);
		menu.BindMenuToItem(PropMenu.menu, menu.MenuItems[2]);
		menu.BindMenuToItem(TattooMenu.menu, menu.MenuItems[3]);
		menu.BindMenuToItem(BadgeMenu.menu, menu.MenuItems[4]);
		menu.BindMenuToItem(DressingRoomAdvancedMenu.menu, menu.MenuItems[5]);
		menu.BindMenuToItem(RecordMenu.menu, menu.MenuItems[6]);
		menu.BindMenuToItem(RecordMenu.menu, menu.MenuItems[7]);
		menu.RefreshIndex();
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		if (index == 0)
		{
			ModelMenu.InitModelMenu();
		}
		if (index == 1)
		{
			ComponentMenu.InitComponentMenu();
		}
		if (index == 2)
		{
			PropMenu.InitPropMenu();
		}
		if (index == 3)
		{
			TattooMenu.InitTattooMenu();
		}
		if (index == 4)
		{
			BadgeMenu.InitBadgeMenu();
		}
		if (index == 5)
		{
			DressingRoomAdvancedMenu.InitAdvancedMenu();
		}
		if (index == 6)
		{
			RecordMenu.InitRecordMenu(_isSave: true);
		}
		if (index == 7)
		{
			RecordMenu.InitRecordMenu(_isSave: false);
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		menu.Visible = false;
		DressingScript.EndDressing();
		// 移除不必要的RefreshIndex调用，减少菜单闪烁
	}
}
